package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Transactional
    @Override
    public void saveWithFlavor(DishDTO dishDTO) {
        /**
         * 保存菜品数据
         * 1.创建Dish对象
         * 2.将dishDTO中的数据拷贝到Dish对象中
         * 3.将Dish对象插入到数据库中
         * 4.从Dish对象中获取到dishFlavors集合
         * 5.遍历dishFlavors集合，设置
         */
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insert(dish);
        //获取当前修改的时间，添加修改时间
        dishDTO.getFlavors().forEach(dishFlavor -> {
            dishFlavor.setDishId(dish.getId());
        });
        dishFlavorMapper.insertBatch(dishDTO.getFlavors());

    }

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        //使用PageHelper进行分页
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page page=dishMapper.page(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void delete(Long[] ids) {
        /**
         * 1.遍历数组获取id
         * 2.如果当前菜品是启售中的，则不能删除
         * 3.如果当前菜品关联了套餐，则不能删除
         * 4.如果当前菜品关联了口味，则需要把口味删除
         */
        for (Long id : ids) {
            Dish dish = dishMapper.findById(id);
            if(dish.getStatus()== StatusConstant.ENABLE){
                //当前菜品是启售中的，则不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
            List<SetmealDish> setmealDishes  = setmealDishMapper.getByDishId(id);
            if(setmealDishes != null && setmealDishes.size() > 0){
                //当前菜品关联了套餐，则不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
            //菜品可以删除
            dishMapper.delete(id);
            dishFlavorMapper.deleteByDishId(id);
        }
    }

    @Override
    public DishVO getByIdWithFlavor(Long id) {
        /**
         * 1.去Dish表中查询Dish
         * 2.去DishFlavor表中查询DishFlavor
         * 3.将Dish和DishFlavor封装到DishDTO中并返回
         */
        Dish dish = dishMapper.findById(id);
        List<DishFlavor> dishFlavors = dishFlavorMapper.findByDishId(id);
        DishVO dishVO = new DishVO();
        //拷贝属性
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    @Transactional
    @Override
    public void updateWithFlavors(DishDTO dishDTO) {
        /**
         * 1.修改Dish表(DishDTO-->Dish)
         * 2.删除dish_flavor表中的当前菜品的口味数据
         * 3.重新插入dish_flavor表
         */
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setUpdateTime(LocalDateTime.now());
        dishMapper.update(dish);
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        List<DishFlavor> flavors=dishDTO.getFlavors();
        flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishDTO.getId()));
        dishFlavorMapper.insertBatch(flavors);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .updateTime(LocalDateTime.now())
                .build();
        dishMapper.update(dish);
    }

    @Override
    public Page<SetmealVO> list(Long categoryId) {
        //根据添加页面点击添加菜品之后根据菜品分类查询该分类所包含的所有菜品
        Page<SetmealVO> pageResult = setmealMapper.list(categoryId);
        return pageResult;
    }
}
